package cn.nuist.controller;

import cn.nuist.entity.User;
import cn.nuist.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Objects;

@Controller
@RequestMapping("user")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping(value = "login")
    public String login(String username, String password, HttpSession session) {
        try {
            User user = userService.login(username, password);
            session.setAttribute("user", user);
            return "redirect:/frame.jsp"; // 确保这里没有附加参数
        } catch (Exception e) {
            e.printStackTrace();
            // 对错误消息进行URL编码，避免中文乱码
            String errorMsg = e.getMessage();
            try {
                errorMsg = java.net.URLEncoder.encode(errorMsg, "UTF-8");
            } catch (Exception ex) {
                errorMsg = "登录失败，请联系管理员";
            }
            return "redirect:/login.jsp?msg=" + errorMsg;
        }
    }

    @RequestMapping("listUser")
    public String listUser(Model model){
        ArrayList<User> users = userService.getAllUser();
        model.addAttribute("users",users);
        return "user/userList";
    }

    @RequestMapping("addUser")
    public String addUser(String name,String username,String password,String rpassword,String role,Model model) throws UnsupportedEncodingException {
        //System.out.println("====================");
        //System.out.println(name + username + password +role +rpassword);
        try{
            userService.addUser(name,username,password,rpassword,role);
            return "redirect:/user/listUser";
        }catch (Exception e){
            model.addAttribute("msg",e.getMessage());
            System.out.println(e.getMessage());
            return "/user/userAdd";
        }
    }

    @RequestMapping("search")
    public String search(String username, RedirectAttributes redirectAttributes){
        //System.out.println("========"+username);
        try {
            User userByName = userService.getUserByName(username);
            return "redirect:/user/show?username="+username;
        }catch (Exception e){
            redirectAttributes.addFlashAttribute("msg",e.getMessage());
            return "redirect:/user/listUser";
        }
    }

    @RequestMapping("show")
    public String show(String username,Model model){
        //System.out.println(username);
        //System.out.println("   8888"+username);
        User userByName = userService.getUserByName(username);
        model.addAttribute("user",userByName);
        return "user/userView";
    }

    @RequestMapping(value = "updateView")
    public String processUpdate(@RequestParam("userId") int userId, Model model) {
        // 处理用户更新逻辑
        //userService.updateUser(user);
        User userById = userService.findUserById(userId);
        //System.out.println("******"+ userById);
        model.addAttribute("user",userById);
        //System.out.println("*****"+userId);
        return "/user/userUpdate";
    }

    @RequestMapping("update")
    public String Update(User user,Model model){
        //System.out.println("*****"+user);
        //System.out.println(id);
        userService.update(user);
        User userByName = userService.getUserByName(user.getUsername());
        model.addAttribute("user",userByName);
        return "user/userView";
    }

    @RequestMapping("delete")
    public String delete(@RequestParam("userId") int userId){
        userService.delete(userId);
        return "redirect:/user/listUser";
    }




}